package com.kimho.cuttrucker.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.cuttrucker.domain.CutTrucker;
import com.kimho.cuttrucker.service.CutTruckerService;
import com.kimho.user.domain.User;
import com.kimho.util.Util;

@Controller
@RequestMapping("/cuttrucker")
public class CutTruckerController {
	
	@Autowired
	private CutTruckerService cutTruckerService;
	
	@RequestMapping("/cut")
	public @ResponseBody Map<String, Object> getCutTruckInhouse(CutTrucker ct){
		Map<String, Object> responseMap = new HashMap<String, Object>();
		try {
			String ids = ct.getObject_id();
			if(ids!=null && !"".equals(ids)){
				String[] id = ids.split(",");//前台传来的是一个以逗号分隔的货物号的字符串
				List<CutTrucker> cutTruckers = new ArrayList<CutTrucker>();
				CutTrucker cutt = null;
				String date = Util.getUserDate("yyyy-MM-dd HH:mm:ss") ;
				for (String string : id) {
					cutt = new CutTrucker();
					cutt.setId(Util.getUUID());
					cutt.setObject_id(string);
					cutt.setCuttrucker_location(ct.getCuttrucker_location());
					cutt.setCuttrucker_staff(ct.getCuttrucker_staff());
					cutt.setCuttrucker_time(date);
					cutt.setLogistics_id(ct.getLogistics_id());
					cutTruckers.add(cutt);
				}
				this.cutTruckerService.addCutTrucker(cutTruckers);
				responseMap.put("success", "cutobj");
				responseMap.put("info", "选择的货物全部接车成功！");
				return responseMap;
			}else if(ct.getLogistics_id()!=null && !ct.getLogistics_id().equals("")){
				this.cutTruckerService.addCutTruckerbyLogistics_id(ct);
				responseMap.put("success", "cutlog");
				responseMap.put("info", "选择的单号接车成功！");
				return responseMap;
			}
			responseMap.put("success", "false");
			responseMap.put("info", "没有选择货物或者单号!");
			return responseMap;
			
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "接车失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}
	
	@RequestMapping(value = "/getallcuttrucker", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> getAllCuttrucker(HttpServletRequest request,
			HttpServletResponse response) {
		
		Map<String, Object> responseMap = new HashMap<String, Object>();
		PageInfo pi = new PageInfo(request);
		pi.setQueryParam("query", request.getParameter("query"));
		pi.setQueryParam("over", "3");//查询出已接车的物件
		if((User)request.getSession().getAttribute("user")!=null && ((User)request.getSession().getAttribute("user")).getPost_id()!=1){//如果登陆用户的较色不是1(经理),就只查询登陆地点的数据
			pi.setQueryParam("login_location", request.getSession().getAttribute("login_localtion"));
		}
		List<CutTrucker> products = this.cutTruckerService.getAllCuttrucker(pi);
		responseMap.put("rows", products);
		responseMap.put("totalCount", products.size());
		return responseMap;
	}
}
